package com.epfresh.supplier.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.*;
import com.epfresh.supplier.entity.*;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BookGoodsMonthActivity extends BaseActivity {


    private TextView tv_time_now,tv_money_now,tv_title,tv_package,tv_number;
    ImageView iv_product;
    ExpandableListView lv_deal_detail;
    BookGoodsMonthExpandListAdapter bookYearGoodsAdapter;

    ArrayList<BookGoodsMonth.SupplyOrderIncomeDayListBean> list = new ArrayList<>();

    String timeTitle,money,timeValue,productId,type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_goods_month);
        ivBack.setVisibility(View.VISIBLE);
        View headView;
        headView = getLayoutInflater().inflate(R.layout.include_book_head_goods, null);


        money = getIntent().getStringExtra("money");
        timeValue = getIntent().getStringExtra("timeValue");
        timeTitle = getIntent().getStringExtra("timeTitle");
        productId = getIntent().getStringExtra("productId");
        type = getIntent().getStringExtra("type");

        toolbarTitle.setText(timeTitle+"商品销售详情");
        iv_product = (ImageView) headView.findViewById(R.id.iv_product);

        tv_time_now = (TextView) headView.findViewById(R.id.tv_time_now);
        tv_money_now = (TextView) headView.findViewById(R.id.tv_money_now);
        tv_time_now.setText(timeTitle);
        tv_money_now.setText(money);

        tv_title = (TextView) headView.findViewById(R.id.tv_title);
        tv_package = (TextView) headView.findViewById(R.id.tv_package);
        tv_number = (TextView)headView.findViewById(R.id.tv_number);

        lv_deal_detail = (ExpandableListView)findViewById(R.id.lv_deal_detail);
        lv_deal_detail.addHeaderView(headView);
        bookYearGoodsAdapter = new BookGoodsMonthExpandListAdapter(this,lv_deal_detail,list);
        //bookYearGoodsAdapter.setOnItemClickListener(this);
        lv_deal_detail.setAdapter(bookYearGoodsAdapter);
        lv_deal_detail.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return true;
            }
        });
        mHttpLoad();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_time:
                // 返回上一页
                toast("弹出时间对话框");
                break;
            case R.id.tv_more_best:

                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("timeValue",timeValue);
        outState.putString("productId",productId);
        outState.putString("money",money);
        outState.putString("timeTitle",timeTitle);
        outState.putString("type",type);
        super.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("queryDate", timeValue);
        map.put("accountId",getUser().getAccountId());
        map.put("specId", productId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth, onRequestListener);
    }

    private OnRequestListener<BookGoodsMonth> onRequestListener = new OnRequestListener<BookGoodsMonth>() {
        @Override
        public BookGoodsMonth jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BookGoodsMonth.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BookGoodsMonth> response, Object tag) {

            hideProgressDialog();
            BookGoodsMonth responseElement = response.getResponseElement();

            tv_title.setText(responseElement.getProductTitle());
            tv_package.setText(responseElement.getPack());
            tv_number.setText(responseElement.getProductCountTotleTitle());
            ImageLoader.getInstance().displayImage(responseElement.getProductImg(), iv_product);

            list.clear();
            list.addAll(responseElement.getSupplyOrderIncomeDayList());
            bookYearGoodsAdapter.notifyDataSetChanged();
        }
    };

}
